package com.gst.cloudygovernance.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gst.cloudygovernance.R;
import com.gst.cloudygovernance.base.BaseFragment;
import com.gst.cloudygovernance.fragment.AlarmFragment;
import com.gst.cloudygovernance.fragment.PersonalFrament;
import com.gst.cloudygovernance.fragment.ReportFragment;
import com.gst.cloudygovernance.fragment.RunFragment;
import com.gst.cloudygovernance.utils.Constants;
import com.gst.cloudygovernance.utils.ToastUtil;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity {
    @Bind(R.id.fl_main_content)
    FrameLayout flMainContent;
    @Bind(R.id.rb_yx)
    RadioButton rbYx;
    @Bind(R.id.rb_bjcl)
    RadioButton rbBjcl;
    @Bind(R.id.rb_tjbb)
    RadioButton rbTjbb;
    @Bind(R.id.rb_jcxx)
    RadioButton rbJcxx;
    @Bind(R.id.rg_bottom_tag)
    RadioGroup rgBottomTag;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_map)
    ImageView ivMap;
    @Bind(R.id.chat)
    ImageView chat;
    private int position;
    private ArrayList<BaseFragment> fragments;
    private BaseFragment mContext;
    private FragmentManager fm;
    private RunFragment runFragment;
    private AlarmFragment alarmFragment;
    private ReportFragment reportFragment;
    private PersonalFrament personalFrament;
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        fm = getSupportFragmentManager();
        if (savedInstanceState != null) {
            runFragment = (RunFragment) fm.findFragmentByTag(Constants.RUNFRAGMENT);
            alarmFragment = (AlarmFragment) fm.findFragmentByTag(Constants.ALARMFRAGMENT);
            reportFragment = (ReportFragment) fm.findFragmentByTag(Constants.REPORTFRAGMENT);
            personalFrament = (PersonalFrament) fm.findFragmentByTag(Constants.PERSONALFRAMENT);
        }
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initFragment();
        initListener();
    }

    /**
     * 检测sha1
     *
     * @param context
     * @return
     */
    public static String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String s = hexString.toString();
            return s.substring(0, s.length() - 1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void initListener() {
        rgBottomTag.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_bjcl:
                        position = 1;//报警
                        ivMap.setVisibility(View.VISIBLE);
                        setSelect(position);
                        break;
                    case R.id.rb_tjbb:
                        position = 2;//报表
                        ivMap.setVisibility(View.GONE);
                        setSelect(position);
                        break;
                    case R.id.rb_jcxx:
                        position = 3;//基础信息
                        ivMap.setVisibility(View.GONE);
                        setSelect(position);
                        break;
                    default:
                        position = 0;//运行情况
                        ivMap.setVisibility(View.VISIBLE);
                        setSelect(position);

                        break;
                }
//                BaseFragment baseFragment = getFragment(position);
//                switchFragment(mContext, baseFragment);
            }
        });
        rgBottomTag.check(R.id.rb_yx);

        //进入定位
        ivMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show(MainActivity.this, "进入地图了", Toast.LENGTH_SHORT);
                startActivity(new Intent(MainActivity.this, MapActivity.class));
            }
        });
        //默认显示第一个fragment数据
        setSelect(0);
    }

    private void setSelect(int position) {
//        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        //每点一次都隐藏一次
        hideFragment();
        switch (position) {
            case 0:
                //运行情况
                if (runFragment == null) {   //目的是为了不让多次实例化 优化处理
                    runFragment = new RunFragment();
//                    LogUtil.e("runFragment被实例化一次");
                    ft.add(R.id.fl_main_content, runFragment, Constants.RUNFRAGMENT);
                }
                ft.show(runFragment);
                break;
            case 1:
                //报警处理
                if (alarmFragment == null) {
                    alarmFragment = new AlarmFragment();
//                    LogUtil.e("alarmFragment被实例化一次");
                    ft.add(R.id.fl_main_content, alarmFragment, Constants.ALARMFRAGMENT);
                }
                ft.show(alarmFragment);
                break;
            case 2:
                //报表
                if (reportFragment == null) {
                    reportFragment = new ReportFragment();
//                    LogUtil.e("reportFragment被实例化一次");
                    ft.add(R.id.fl_main_content, reportFragment, Constants.REPORTFRAGMENT);
                }
                ft.show(reportFragment);
                break;
            case 3:
                //个人信息
                if (personalFrament == null) {
//                    LogUtil.e("personalFrament被实例化一次");
                    personalFrament = new PersonalFrament();
                    ft.add(R.id.fl_main_content, personalFrament, Constants.PERSONALFRAMENT);
                }
                ft.show(personalFrament);
                break;
        }
        ft.commit();
    }

    private void hideFragment() {
        if (runFragment != null) {
            ft.hide(runFragment);
        }
        if (alarmFragment != null) {
            ft.hide(alarmFragment);
        }
        if (reportFragment != null) {
            ft.hide(reportFragment);
        }
        if (personalFrament != null) {
            ft.hide(personalFrament);
        }
    }

    private void initFragment() {
        RunFragment runFragment = new RunFragment();
        fragments = new ArrayList<>();
        fragments.add(runFragment);
        fragments.add(new AlarmFragment());
        fragments.add(new ReportFragment());
        fragments.add(new PersonalFrament());
    }

    /**
     * @param position
     * @return
     */
    private BaseFragment getFragment(int position) {
        if (fragments != null && fragments.size() > 0) {
            BaseFragment baseFragment = fragments.get(position);
            return baseFragment;
        }
        return null;
    }

    @OnClick(R.id.chat)
    public void onViewClicked() {

    }

//    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
//        if (mContext != nextFragment) {
//            mContext = nextFragment;
//            if (nextFragment != null) {
//                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//                //判断nextFragment是否添加
//                if (!nextFragment.isAdded()) {
//                    //隐藏当前Fragment
//                    if (fromFragment != null) {
//                        transaction.hide(fromFragment);
//                    }
//                    transaction.add(R.id.fl_main_content, nextFragment).commit();//replace会重叠还是？有待解决
//                } else {
//                    //隐藏当前Fragment
//                    if (fromFragment != null) {
//                        transaction.hide(fromFragment);
//                    }
//                    transaction.show(nextFragment).commit();
//                }
//            }
//        }
//    }

}
